﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WaterKit.Base
{
    public class Log4C
    {
        /// <summary>
        /// 捕获异常错误日志
        /// </summary>
        public static void WriteErrLog(string errTitle, Exception ex)
        {
            string dir=AppDomain.CurrentDomain.BaseDirectory + "\\log" ;
            string path = dir+ "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            if (!File.Exists(path))
            {
                File.Create(path).Close();
            }

            StringBuilder strBuilderErrorMessage = new StringBuilder();
            strBuilderErrorMessage.Append("________________________________________________________________________________________________________________\r\n");
            strBuilderErrorMessage.Append("日期:" + System.DateTime.Now.ToString() + "\r\n");
            strBuilderErrorMessage.Append("错误标题:" + errTitle + "\r\n");
            strBuilderErrorMessage.Append("错误信息:" + ex.Message + "\r\n");
            strBuilderErrorMessage.Append("错误内容:" + ex.StackTrace + "\r\n");
            using (StreamWriter sw = File.AppendText(path))
            {
                sw.Write(strBuilderErrorMessage);
                sw.Flush();
                sw.Close();
            }
        }

        /// <summary>
        /// 日志
        /// </summary>
        /// <param name="logTitle"></param>
        /// <param name="logContent"></param>
        public static void WriteLog(string logTitle, string logContent)
        {
            string dir = AppDomain.CurrentDomain.BaseDirectory + "\\log";
            string path = dir + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            if (!File.Exists(path))
            {
                File.Create(path).Close();
            }

            StringBuilder strBuilderErrorMessage = new StringBuilder();
            strBuilderErrorMessage.Append("________________________________________________________________________________________________________________\r\n");
            strBuilderErrorMessage.Append("标题:" + logTitle + "\r\n");
            strBuilderErrorMessage.Append("时间:" + DateTime.Now.ToString() + "\r\n");
            strBuilderErrorMessage.Append("内容" + logContent + "\r\n");
            using (StreamWriter sw = File.AppendText(path))
            {
                sw.Write(strBuilderErrorMessage);
                sw.Flush();
                sw.Close();
            }
        }
    }
}
